home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’91 / AliasThis! / AliasThisƒ / src / SampleErrors.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-24  |  5.5 KB  |  213 lines  |  [TEXT/MPS ]

  1. #include "SampleDefs.h"
  2. #include "Sample.h"
  3. #include "SampleErrors.h"
  4.  
  5. #include <Memory.h>
  6. #include <Packages.h>
  7. #include <Fonts.h>
  8. #include <ToolUtils.h>
  9.  
  10. #include <String.h>
  11.  
  12. #define POINT_TO_LONG(aPoint) (* (long *) &(aPoint) )
  13.  
  14.  
  15. #pragma segment Main
  16. /***************************************************************************************
  17.  
  18.     LongIntToHex
  19.  
  20.     Converts a long into a hexadecimal string.
  21.  
  22. ****************************************************************************************/
  23. void LongIntToHex(long decNumber, Str255 hexNumber, short noOfDigits)
  24. {
  25.     short    i;
  26.     char    realNumberOfDigits;
  27.  
  28.     if (noOfDigits > 8)
  29.         realNumberOfDigits = 8;
  30.     else
  31.         realNumberOfDigits = noOfDigits;
  32.  
  33.     hexNumber[0] = realNumberOfDigits;
  34.     for (i = realNumberOfDigits; i >= 1; --i) {
  35.         hexNumber[i] = "0123456789ABCDEF"[(decNumber & 0x0F)];
  36.         decNumber >>= 4;
  37.     }
  38. }
  39.  
  40. #pragma segment Main
  41. /***************************************************************************************
  42.  
  43.     DisplayErrRec
  44.  
  45.     This routine is called by the Dialog Manager to image our custom item. This
  46.     custom item displays an error number, an event record, and a block of text
  47.     explaining what occured and where.
  48.  
  49. ****************************************************************************************/
  50. pascal void DisplayErrRec(DialogPtr dptr, short item)
  51. {
  52. #pragma unused (item)        /* unused formal parameters */
  53.     short            itemType;
  54.     Handle            itemHandle;
  55.     Rect            itemRect;
  56.     short            fnum;
  57.     GrafPtr            currentPort;
  58.     PortFontInfo    pfi;
  59.     FontInfo        fi;
  60.     ErrorRec        *er;
  61.     Str255            numstr;
  62.     short            lheight;
  63.     Str255            OSString = "\p  1234";
  64.     Str255            tempString;
  65.  
  66.     /* get the port and save the font info */
  67.  
  68.     GetPort(¤tPort);
  69.     pfi.txFont = currentPort->txFont;
  70.     pfi.txSize = currentPort->txSize;
  71.     pfi.txFace = currentPort->txFace;
  72.  
  73.     /* get the useritem rect */
  74.  
  75.     GetDItem(dptr, A_USERITEM, &itemType, &itemHandle, &itemRect);
  76.  
  77.     /* set the font information we need */
  78.     GetFNum("\pHelvetica", &fnum);
  79.     TextFont(fnum);
  80.     TextSize(12);
  81.     TextFace(normal);
  82.  
  83.     /* get the font information so we know the line height */
  84.     GetFontInfo(&fi);
  85.     lheight = fi.ascent + fi.descent + fi.leading;
  86.  
  87.     /* indent by insetting the rectangle */
  88.     InsetRect(&itemRect, 2, 2);
  89.  
  90.     /* Get the event info */
  91.     er = (ErrorRec *)GetWRefCon(dptr);
  92.  
  93.     if (er != nil) {
  94.         /* Display message that an error occurred. */
  95.         MoveTo(itemRect.left, itemRect.top + fi.ascent);
  96.         GetIndString(tempString, ERR_DLOG_ID, sErrorOccurred);
  97.         DrawString(tempString);
  98.         itemRect.top = itemRect.top + lheight;
  99.  
  100.         if (er->err != 0) {
  101.             /* draw the error number */
  102.             MoveTo(itemRect.left, itemRect.top + fi.ascent);
  103.             GetIndString(tempString, ERR_DLOG_ID, sErrorNumber);
  104.             NumToString(er->err, numstr);
  105.             DrawString(tempString); DrawString(numstr);
  106.             itemRect.top = itemRect.top + lheight;
  107.         }
  108.  
  109.         if (er->event != nil) {
  110.             /* draw event.what */
  111.             MoveTo(itemRect.left, itemRect.top + fi.ascent);
  112.             GetIndString(tempString, ERR_DLOG_ID, sEventWhat);
  113.             NumToString(er->event->what, numstr);
  114.             DrawString(tempString); DrawString(numstr);
  115.             itemRect.top = itemRect.top + lheight;
  116.  
  117.             /* draw event.message */
  118.             MoveTo(itemRect.left, itemRect.top + fi.ascent);
  119.             GetIndString(tempString, ERR_DLOG_ID, sEventMessage);
  120.             LongIntToHex(er->event->message, numstr, 8);
  121.             BlockMove((Ptr) &er->event->message, &OSString[3], 4);
  122.             DrawString(tempString); DrawString(numstr); DrawString(OSString);
  123.             itemRect.top = itemRect.top + lheight;
  124.  
  125.             /* draw event.where */
  126.             MoveTo(itemRect.left,itemRect.top + fi.ascent);
  127.             GetIndString(tempString, ERR_DLOG_ID, sMessageID);
  128.             /* need to convert the where into a long */
  129.             LongIntToHex(POINT_TO_LONG(er->event->where), numstr, 8);
  130.             BlockMove((Ptr) &er->event->where, &OSString[3], 4);
  131.             DrawString(tempString); DrawString(numstr); DrawString(OSString);
  132.             itemRect.top = itemRect.top + lheight;
  133.         }
  134.  
  135.         if (er->message != nil)
  136.             TextBox(er->message, strlen(er->message), &itemRect, teFlushDefault);
  137.     }
  138.  
  139.     /* restore port font information */
  140.     TextFont(pfi.txFont);
  141.     TextSize(pfi.txSize);
  142.     TextFace(pfi.txFace);
  143.  
  144. }
  145.  
  146.  
  147. #pragma segment Main
  148. /***************************************************************************************
  149.  
  150.     DisplayError
  151.  
  152.     If an error occurs this routine is called. It displays the error, the contents
  153.     of the event record, and a block of explanatory text.
  154.  
  155. ****************************************************************************************/
  156. void DisplayError( OSErr err, EventRecord *evt, char *message )
  157. {
  158.     DialogPtr    dptr;
  159.     Handle        itemHandle;
  160.     short        itemType;
  161.     Rect        itemRect;
  162.     ErrorRec    er;
  163.     short        item;
  164.  
  165.     dptr = GetNewDialog(ERR_DLOG_ID, nil, (WindowPtr)-1L);
  166.  
  167.     if (dptr) {
  168.         GetDItem(dptr,A_USERITEM,&itemType,&itemHandle,&itemRect);
  169.         SetDItem(dptr,A_USERITEM,itemType,(Handle)DisplayErrRec,&itemRect);
  170.     
  171.         er.err = err;
  172.         er.event = evt;
  173.         er.message = message;
  174.     
  175.         SetWRefCon( dptr, (long)&er );
  176.         ShowWindow( dptr );
  177.         do {
  178.             ModalDialog(nil,&item);
  179.         } while ( item != ok );
  180.     
  181.         DisposDialog(dptr);
  182.     }
  183. }
  184.  
  185.  
  186. #pragma segment Main
  187. /***************************************************************************************
  188.  
  189.     ShowElapsedTime
  190.  
  191. ****************************************************************************************/
  192. void ShowElapsedTime(long elapsedTime)
  193. {
  194.     DialogPtr    dptr;
  195.     short        item;
  196.     Str255        tempString1;
  197.     Str255        tempString2;
  198.  
  199.     dptr = GetNewDialog(ELAPSED_DLOG_ID, nil, (WindowPtr)-1L);
  200.  
  201.     if (dptr) {
  202.         ShowWindow(dptr);
  203.         GetIndString(tempString1, ELAPSED_DLOG_ID, sElapsedTime);
  204.         NumToString(elapsedTime, tempString2);
  205.         ParamText(tempString1, tempString2, nil, nil);
  206.         do {
  207.             ModalDialog(nil, &item);
  208.         } while ( item != ok );
  209.     
  210.         DisposDialog(dptr);
  211.     }
  212. }
  213.